<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/21
 * Time: 9:47
 * 产品入库
 */

require('../include/init.inc.php');
$action = $id = $products_id = $model = $num = $add_status = $pg = $name = $price = $desc = $currency = $size = $color = $type = '';
extract($_REQUEST, EXTR_IF_EXISTS);

$product = array();
if ($action == 'modify_rk') {
    $product = AdminProducts::getProductById($id);
    $getType = explode("_", $product['products_model']);
    array_push($product, $getType[0]);
}

if (Common::isPost()) {
    if ($model && $products_id) {
        $product = AdminProducts::getProductById($products_id);
        $data = array("products_sale_num" => $num + $product['products_sale_num'], "products_add_date" => date('Y-m-d H:i:s'), "products_add_status" => $add_status, "products_evaluate" => $pg);
        $result = AdminProducts::update($products_id, $data);
    } else {

        if (!(preg_match('/(?=.*[A-Z]).*/', $type) && strlen($type) == 2)) {
            Common::exitWithError(ErrorMessage::TYPE_ILLEGAL, "entpot/products_add.php");
        }

        if(intval($num) <= 0){
            Common::exitWithError(ErrorMessage::NUM_ILLEGAL, "entpot/products_add.php");
        }

        $lastedInsertId = AdminId::lastedInsertId(array("createtime" => time()));
        $model_name = $type . "_" . $lastedInsertId . "_" . Common::getRandChar(4);
        $data = array("products_model" => $model_name, "products_name" => $name, "products_price" => $price, "products_description" => $desc, "products_currency" => $currency, "products_evaluate" => $pg, "products_size" => $size, "products_color" => $color, "products_sale_num" => $num, "products_add_date" => date('Y-m-d H:i:s'), "products_add_status" => $add_status);
        $result = AdminProducts::add($data);
    }


    if ($result) {
        SysLog::addLog(UserSession::getUserName(), 'Add', 'ProductsAdd', $name, json_encode($data));
        Common::exitWithSuccess('入库完成', 'entpot/products.php');
    } else {
        OSAdmin::alert("error");
    }
}

$confirm_html = OSAdmin::renderJsConfirm("icon-remove");
Template::assign('osadmin_action_confirm', $confirm_html);
Template::assign('_GET', $product);
Template::display('entpot/products_add.tpl');